package com.shuma.userservice.config;

import com.shuma.userservice.fillter.JwtAuthFilter;
import com.shuma.userservice.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class MvcConfig extends WebSecurityConfigurerAdapter {

    private final JwtTool jwtTool;

    public MvcConfig(JwtTool jwtTool) {
        this.jwtTool = jwtTool;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/user/login", "/user/register").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(new JwtAuthFilter(jwtTool), UsernamePasswordAuthenticationFilter.class);
    }
}
